<?php

namespace app\task;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

use api\portal\service\UserInfoService;

use app\user\model\UserModel;
use app\portal\model\UserPromoteRelationModel;
use app\portal\model\UserRewardDetailModel;
use app\portal\model\UserLevelDefineModel;

class SendReward extends Command
{
    protected function configure()
    {
        $this->setName('user:level:update')
            ->setDescription('Let us begin to send reward.');
    }

    protected function execute(Input $input, Output $output)
    {
        // 从UserRewardDetailModel中找到昨天用户获得奖励的总数 以用户id为分组
        $userModel = new UserModel();
        $userLevelDefineModel = new UserLevelDefineModel();
        $userPromoteRelationModel = new UserPromoteRelationModel();
        $userRewardDetailModel = new UserRewardDetailModel();
        $userRewardDetailList = $userRewardDetailModel->where('create_time', '>=', strtotime(date('Ymd')) - 86400)
            ->where('create_time', '<', strtotime(date('Ymd')))
            ->where('reward_type', 3)
            ->field('user_id, sum(reward_count) as reward')
            ->group('user_id')
            ->select();

        // 遍历用户奖励列表 逐个用户找到其推广关系的父用户 根据父用户的推广关系表中的等级定义表中的奖励比例计算奖励 并插入到UserRewardDetailModel中 再更新父用户的余额 再找到父用户的父用户 同样计算奖励 插入到UserRewardDetailModel中        
        foreach ($userRewardDetailList as $userRewardDetail) {
            $userPromoteRelation = $userPromoteRelationModel->where('user_id', $userRewardDetail['user_id'])->find();
            if (empty($userPromoteRelation)) {
                continue;
            }

            if ($userPromoteRelation['parent_id'] > 0) {
                // 奖励给到父用户
                $parentId = $userPromoteRelation['parent_id'];
                $userParent = $userModel->where('id', $parentId)->find();
                if (empty($userParent) || $userParent['user_level'] <= 1) {
                    continue;
                }
                $userParentLevelDefine = $userLevelDefineModel->where('user_level', $userParent['user_level'])->find();
                if (empty($userParentLevelDefine)) {
                    continue;
                }
                $userParentRewardPercent = $userParentLevelDefine['reward_percent'];
                $userParentRewardCount = number_format($userRewardDetail['reward'] * $userParentRewardPercent / 100 / 100, 2);
                
                // 写入奖励记录并加给用户
                $userInfoService = new UserInfoService();
                $remark = '上级奖励 ' . $userRewardDetail['reward'] . '*(' . $userParentRewardPercent/100 . '%)';
                $userInfoService->rewardAddAndLog($parentId, $userParentRewardCount, 2, $userRewardDetail['user_id'], $remark);

                // 奖励给到父用户的父用户 注意级差
                $userParentPromoteRelation = $userPromoteRelationModel->where('user_id', $parentId)->find();
                if (empty($userParentPromoteRelation)) {
                    continue;
                }
                
                if ($userParentPromoteRelation['parent_id'] > 0) {
                    $grandParentId = $userParentPromoteRelation['parent_id'];
                    $userGrandParent = $userModel->where('id', $grandParentId)->find();
                    if (empty($userGrandParent) || $userGrandParent['user_level'] <= 1) {
                        continue;
                    }
                    $userGrandParentLevelDefine = $userLevelDefineModel->where('user_level', $userGrandParent['user_level'])->find();
                    if (empty($userGrandParentLevelDefine)) {
                        continue;
                    }
                    $userGrandParentRewardPercent = $userGrandParentLevelDefine['reward_percent'];
                    if ($userParentRewardPercent > $userGrandParentRewardPercent) {
                        continue;
                    }
                    $userGrandParentRewardRealPercent = $userGrandParentRewardPercent - $userParentRewardPercent;
                    $userGrandParentRewardCount = number_format($userRewardDetail['reward'] * $userGrandParentRewardRealPercent / 100 / 100, 2);

                    $remark = '上上级奖励 ' . $userRewardDetail['reward'] . '*(' . $userGrandParentRewardPercent/100 . '%-' . $userParentRewardPercent/100 . '%)';
                    $userInfoService->rewardAddAndLog($grandParentId, $userGrandParentRewardCount, 2, $userRewardDetail['user_id'], $remark);
                }
            }
        }
    }
}
